#ifndef __UDSF_STACK_C__
#define __UDSF_STACK_C__

#include "app.h"
/*
*   notice:
*   UDSF 同UDF编程：不可调用cstd-lib,只可调用"sdk_ifs_udk_cfg.h"、"sys_api.h"中列出的API函数。
*   UDSF文件内函数均需使用static修饰符，且UDF中以#include "udsf_xxx.c"形式引用
*/

#define STACK_SIZE (0x500) // 1280B


static uint32_t udsf_stack_get_top(void) __attribute__((unused));
static uint32_t udsf_stack_get_bottom(void) __attribute__((unused));

static uint32_t udsf_stack_get_top(void)
{
	return *((uint32_t*)0);
}
static uint32_t udsf_stack_get_bottom(void)
{
	return *((uint32_t*)0) - STACK_SIZE;
}

static uint32_t udsf_stack_rt_percent_get(uint32_t *p_local_var,uint32_t bottom_addr ){
	
	return ((uint32_t)p_local_var - bottom_addr - 4)*100/STACK_SIZE;
}

#if 0

#define MAGIC_WORD (0xdeadbeef)

static void udsf_stack_fill(uint32_t* p_bottom)
{
	uint32_t data = MAGIC_WORD;
	int len = ((uint32_t)&data - (uint32_t)p_bottom)/4 - 4;  // 不清楚入栈顺序 多减4个
	
	for( int i=0;i< len;i++ ){
		p_bottom[i] = data;
	}
}

static uint32_t udsf_stack_max_use_percent_get(uint32_t* p_bottom ){	
	uint32_t percent = 0;
	for( int i = 0;i<STACK_SIZE/4;i++ ){
		if( p_bottom[i] != MAGIC_WORD ){
			percent = (STACK_SIZE - 4*i)*100/STACK_SIZE;
			break;
		}
	}
	return percent;
}
#endif

#endif
